package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;
    @Autowired
    private StrategyTagMapper tagMapper;
    @Autowired
    private StrategyCatalogMapper catalogManager;


    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public void saveOrUpdate(StrategyDetail detail, String tags) {
        //文章内容
        StrategyContent content = detail.getStrategyContent();
        detail.setSummary("Ai正在创作中");

        //设置目的地(文章,明细)
        Long catalogId = detail.getCatalog().getId();
        StrategyCatalog catalog=catalogManager.selectByPrimaryKey(catalogId);
        //创建目的地对象
        Destination dest = new Destination();
        dest.setId(catalog.getDestId());
        detail.setDest(dest);


        if (detail.getId() == null) {
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);

            //保存文章内容
            content.setId(detail.getId());
            contentMapper.insert(content);
        } else {
            strategyDetailMapper.updateByPrimaryKey(detail);
            content.setId(detail.getId());
            contentMapper.updateByPrimaryKey(content);
        }

        //处理标签
        if(tags!=null){
         //先删除就关系,在保存新关系
            strategyDetailMapper.deleteRelation(detail.getId());
            //字符串拆分.
            String[] ts = tags.split(",");
            for (String tagName : ts) {
                StrategyTag tag=tagMapper.selectByName(tagName);
                if(tag==null){ //数据库中不存在.
//                     tag = new StrategyTag();
                    tag.setName(tagName);
                    tagMapper.insert(tag);

                }
                //当前标签和当前文章的关系多对多
                strategyDetailMapper.insertRelation(detail.getId(),tag.getId());
            }


        }

    }

    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    public StrategyContent getContent(Long id) {
        StrategyContent content = contentMapper.selectByPrimaryKey(id);
        return content;
    }

    public void changeState(Long id, Integer state) {
        strategyDetailMapper.changeState(id,state);
    }

    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    public List<StrategyDetail> listViewnumTop3(Long destId) {

        return strategyDetailMapper.selectViewnumTop3( destId);
    }

    public void updateStats(StrategyDetail detail) {
        strategyDetailMapper.updateStats(detail);
    }


}
